/**
 *
 */
package demo.tellurium.module

import groovy.lang.MetaClass;

import demo.tellurium.framework.BaseUIModule;

/**
 * @author roger.xu
 *
 */
class DropDownListModule extends BaseUIModule {

    private static final String MODULE_NAME = "DropDownExample";
    public static final String URL_KEY = "demo.list.url";

    @Override
    public String getModuleName() {
        return MODULE_NAME;
    }

    @Override
    public void defineUi() {
        ui.Container(uid: "DropDownExample") {
            // DropDownList
            Container(uid: "DropDownList") {
                Container(uid: "ComboBox", clocator: [widgetid: "fruit"]) {
                    Div(uid: "ArrowButton", clocator: [class: "dijitArrowButton"], respond: ["mouseDown", "mouseUp", "mouseEnter", "mouseLeave"])
                    Container(uid: "InputField", clocator: [tag: "div", class: "dijitInputField"]) {
                        InputBox(uid: "InputTextBox", clocator: [tag: "input", type: "text"])
                    }
                }
                Container(uid: "ListPopUp", clocator: [tag: "div", dijitpopupparent: "fruit"]) {
                    List(uid: "OptionList", clocator: [tag: "ul"], separator: "li:visible") {
                        UrlLink(uid: "{all}", respond: ["mouseDown", "mouseUp", "mouseOver"]) // actually it is not a link, we just need its "click" event here.
                    }
                }
            }
        }
    }

    /* ---- DropDownList ---------------*/
    public void doSelectItem(int index) {
        def widgetName = "DropDownList";
        def widgetUid = String.format("%s.%s", getModuleName(), widgetName);

        // click arrow button
        def comboboxUid = String.format("%s.ComboBox", widgetUid);
        def arrowButtonUid = String.format("%s.ArrowButton", comboboxUid);
        click arrowButtonUid
        pause 500

        // click option list
        def listUid = String.format("%s.ListPopUp.OptionList", widgetUid);
        int listSize = getListSize(listUid);
        console.debug(listUid, listSize);

        if (index == RANDOM_INDEX) {
            index = getRandomIndex(listSize);
        }

        def optionUid = String.format("%s[%d]", listUid, index);

        mouseOver optionUid
        pause 1000

        click optionUid
        pause 500
    }

    public String getDisplayValue() {
        def widgetName = "DropDownList";
        return getComboBoxValue(widgetName);
    }

}
